/**
*动环监控 js
*/ 

var	envMontorDS;
var envMontorFormPanel;
function doEnvMontorDelete(id)
{
	Ext.MessageBox.confirm('确认删除', '确认删除该信息?', 
	function(btn) {
		if(btn == 'yes') // 选中了是按钮	
		{
			JEnvMontorWeb.deleteEnvMontorBO(id,function(data)
			{
				if(data)
				{ 
					Ext.Msg.alert('成功',"操作成功！"); 
					envMontorDS.load({params:{start:0,limit:17,'<%=Constant.BTS_CODE%>':'<%=strbtscode%>'}});	 
					//envMontorFormPanel.getForm().reset();			
				}else
				{
					Ext.Msg.alert("失败","操作失败!");
				}
			})
		}
	})	
}


Ext.onReady(function(){  
	Ext.QuickTips.init();
	Ext.form.Field.prototype.msgTarget = 'side';

	var envMontorType = Ext.data.Record.create([								
							   {name: 'id', type: "string"},
							   {name: 'btscode', type: "string"},
							   {name: 'btsname', type: "string"},
							   {name: 'manufacturer', type: "string"},
							   {name: 'type', type: "string"}, 
							   {name: 'monobj', type: "string"}, 
							   {name: 'transType', type: "string"}, 
							   {name: 'opentime', type: "date"},
							   {name: 'maintenance', type: 'string'},
							   {name: 'refreshtime',  type: 'date'},
							   {name: 'memo',  type: 'string'},
							   {name: 'transPort',  type: 'string'},
							   {name: 'monWorkSta',  type: 'string'}
						 ]);
	var envMontorCM = new Ext.grid.ColumnModel(
			[
				new Ext.grid.CheckboxSelectionModel({singleSelect: true}), 	            
				{header: "流水号", sortable: true, dataIndex: 'id',align:'left'},
				{header: "基站编号", sortable: true, dataIndex: 'btscode',align:'left'},
				{header: "基站名称",  sortable: true, dataIndex: 'btsname',align:'left'},	
				{header: "规格型号",  sortable: true, dataIndex: 'type',align:'left'},	
				{header: "厂家",  sortable: true, dataIndex: 'manufacturer',align:'left'},
				{header: "监控对象",  sortable: true, dataIndex: 'monobj',align:'left'},
				//{header: "传输方式",  sortable: true, dataIndex: 'transType',align:'left'},
				{header: "传输端口",  sortable: true, dataIndex: 'transPort',align:'left'},						    
				{header: "启用日期",  width: 150,sortable: true, renderer: Ext.util.Format.dateRenderer('Y-m-d'),dataIndex: 'opentime',align:'left'} 				
			]
		);				 
	
	function showDelete(id)
	{
		return "<input type='button' value='删除' class='x-btn button' onclick='doEnvMontorDelete(\"" + id  +   "\")'>";  
	}
	
	function formatFence(value) 
	{
		var strState = "";
		if(value == "0")
			strState = '<span style="color:red;">' + "否" + '</span>';
		else  
			strState = '是'; 		
		return strState;
	}
	
	envMontorDS = new Ext.data.Store({
				proxy: new Ext.data.DWRProxy(JEnvMontorWeb.getEnvMontorList, true),	
				reader: new Ext.data.ListRangeReader({
					totalProperty: 'totalSize',
					id: 'id'
				}, envMontorType),
				sortInfo:{field:'id', direction:'ASC'}// 排序信息
			});

	
	var towerMountedGridDiv = Ext.get("mainTabPanelDIV");
	var envMontorGrid = new Ext.grid.GridPanel({			 	
			ds: envMontorDS,
			cm: envMontorCM, 
			sm: new Ext.grid.RowSelectionModel({
                    singleSelect: true,
                    listeners: {
                        rowselect: function(sm, row, rec) {
                            envMontorFormPanel.getForm().loadRecord(rec);
                        }
                    }
                }),
			autoWidth : true,  
			autoScroll : true,  
			autoHeight　: false,
			region : 'center',
			height: 400,
			minSize: 200,		
			frame:true,
			collapsible:false, 
			enableDragDrop:false, 		
			bbar: new Ext.PagingToolbar({
					pageSize: 17,
					store: envMontorDS,
					displayInfo: true,
					displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
					emptyMsg: "没有记录"
				})
		});
	envMontorDS.on('load', function() {
		envMontorGrid.getSelectionModel().selectFirstRow(); 
	});	
	 	
	var typeStore = new Ext.data.Store({  
		        proxy:new Ext.data.DWRProxy(JCommWeb.getDirctoryByType,{}),  
		        reader:new Ext.data.DWRArrayReader(
						{},
						new Ext.data.Record.create([  
							{name: 'text', mapping:1},       
							{name: 'value',mapping:0}                   
						  ])
						)  
		    }); 
	        
	var transTypeStore  = new Ext.data.Store({  
		        proxy:new Ext.data.DWRProxy(JCommWeb.getDirctoryByType,{}),  
		        reader:new Ext.data.DWRArrayReader(
						{},
						new Ext.data.Record.create([  
							{name: 'text', mapping:1},       
							{name: 'value',mapping:0}                   
						  ])
						)  
		    }); 
	
	var transTypeCombo = new Ext.form.ComboBox({	   
		fieldLabel: '使用传输方式',
		name: 'transType',	
		width:150,
		valueField :"value",
	    displayField: "text",
	    mode: 'local',
		selectOnFocus:true,                    
		forceSelection: true, 
		hiddenName:'transType',
		triggerAction: 'all', 
		editable:false, 
		disabled:true,
		store: transTypeStore
	});
	transTypeStore.on('beforeload', function() {
			  transTypeStore.baseParams = {
					DIRECTORY_TYPE:'08'   
			  };
		});	
	transTypeStore.load({params:{DIRECTORY_TYPE:'08'}});
	
	
	var monWorkStaStore  = new Ext.data.Store({  
			proxy:new Ext.data.DWRProxy(JCommWeb.getDirctoryByType,{}),  
			reader:new Ext.data.DWRArrayReader(
					{},
					new Ext.data.Record.create([  
						{name: 'text', mapping:1},       
						{name: 'value',mapping:0}                   
					  ])
					)  
		});
	var monWorkStaCombo = new Ext.form.ComboBox({	   
		fieldLabel: '工作状态',
		name: 'monWorkSta',	
		width:150,
		valueField :"value",
	    displayField: "text",
	    mode: 'local',
		selectOnFocus:true,                    
		forceSelection: true, 
		hiddenName:'monWorkSta',
		triggerAction: 'all', 
		editable:false, 
		disabled:true,
		store: monWorkStaStore
	});
	monWorkStaStore.on('beforeload', function() {
			  monWorkStaStore.baseParams = {
					DIRECTORY_TYPE:'26'   
			  };
		});	
	monWorkStaStore.load({params:{DIRECTORY_TYPE:'26'}});
	
	
	
	envMontorFormPanel = new Ext.form.FormPanel({
		region     : 'south', 
		bodyStyle  : 'padding: 10px; background-color: #DFE8F6',
		labelWidth : 130,  
		height: 190,
		minSize: 200,
		maxSize: 200,
		frame:true,
		buttonAlign:'right',
		layout:'column', 
		items:[
			 {
				columnWidth:.33,
				layout: 'form', 
				labelAlign:'right',					
				labelWidth:100,	 
				items: [
					{
						xtype:'hidden',
						fieldLabel: '流水号',
						width:150,
						name: 'id'
					}, {
						xtype:'textfield',
						readOnly: true,
						fieldLabel: '基站编号',
						width:150,
						disabled:true,
						name: 'btscode'
					}, {
						xtype:'textfield',
						fieldLabel: '基站名称',
						width:150,
						readOnly: true,
						disabled:true,
						name: 'btsname'
					}, {
						xtype:'textfield',
						fieldLabel: '监控对象',
						width:150,
						disabled:true,
						name: 'monobj'
					}, {
						xtype:'datefield',
						fieldLabel: '启用时间',
						width:150,
						format : "Y-m-d",
						allowBlank:false,
						disabled:true,
						name: 'opentime'
					}
					]
			 },
			 {
				columnWidth:.33,
				layout: 'form',  
				labelAlign:'right',	
				labelWidth:110,	 
				items: [
					{
						xtype:'textfield',
						fieldLabel: '规格型号',
						width:150,
						disabled:true,
						name: 'type'
					}, {
						xtype:'textfield',
						fieldLabel: '厂家',
						width:150,
						disabled:true,
						name: 'manufacturer'
					}
					, {
						xtype:'textfield',
						fieldLabel: '使用传输端口号',
						width:150,
						value:1,
						disabled:true,
						name: 'transPort'
					}
					, {
						xtype:'textfield',
						fieldLabel: '更换及维修记录',
						width:150,
						disabled:true,
						name: 'maintenance'
					}]
			 },
			 {
				columnWidth:.33,
				layout: 'form',  
				labelAlign:'right',	
				labelWidth:110,	 
				items: [
					 transTypeCombo
					,monWorkStaCombo					
					,{
						xtype:'textarea',
						fieldLabel: '备注',
						width:150,
						disabled:true,
						name: 'memo'
					}]
			 }
			]	
	});



	//Simple 'border layout' panel to house both grids
	
	var displayPanel = new Ext.Panel({
		width    : towerMountedGridDiv.getComputedWidth(),
		height   : 500,
		layout   : 'border',
		renderTo : 'EnvMontorTab',
		items    : [
			envMontorGrid,
			envMontorFormPanel
		]
	});
	
	
	function saveEnvMontor()
	{
		if (!envMontorFormPanel.getForm().isValid())
			return;	
			
		var form = envMontorFormPanel.getForm().getValues(false);	
		EnvMontorBO = {};
		EnvMontorBO.id = form.id;
		EnvMontorBO.btscode = form.btscode;
		EnvMontorBO.btsname= form.btsname;
		EnvMontorBO.type = form.type;
		EnvMontorBO.manufacturer = form.manufacturer;
		EnvMontorBO.monobj = form.monobj;
		EnvMontorBO.transType = form.transType;
		EnvMontorBO.transPort = form.transPort;		
		EnvMontorBO.maintenance = form.maintenance;
		EnvMontorBO.memo = form.memo; 		
		EnvMontorBO.monWorkSta = form.monWorkSta;		 
		
		var opentimeDate = new Date();	
		var arrPlanTime = form.opentime.split("-");		
				
		var yearOth = arrPlanTime[0];
		var monthOth = arrPlanTime[1];
		var dayOth = arrPlanTime[2];
		opentimeDate.setFullYear(yearOth,monthOth-1,dayOth);
		
		EnvMontorBO.opentime = opentimeDate;
	   			
		JEnvMontorWeb.saveEnvMontorBO(EnvMontorBO,function(data)
		{
			if(data)
			{ 
				Ext.Msg.alert('成功',"操作成功！"); 
				loadDate(envMontorDS,envMontorFormPanel); 
			}else
			{
				Ext.Msg.alert("失败","操作失败!");
			}
		})
			
	}
		
	
	loadDate(envMontorDS,envMontorFormPanel);
	
})


